package com.nbjtjc.safe.service.edu;

import cn.hutool.core.util.ObjectUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.nbjtjc.safe.bean.Result;
import com.nbjtjc.safe.mapper.edu.EduRequirementInvestigationMapper;
import com.nbjtjc.safe.mapper.edu.EduRequirementItemsSurveyResultsMapper;
import com.nbjtjc.safe.mapper.edu.EduRequirementSurveyItemMapper;
import com.nbjtjc.safe.mapper.edu.EduRequirementUserSurveyResultsMapper;
import com.nbjtjc.safe.model.edu.EduRequirementItemsSurveyResults;
import com.nbjtjc.safe.model.edu.EduRequirementSurveyItem;
import com.nbjtjc.safe.model.edu.EduRequirementUserSurveyResults;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class EduRequirementUserSurveyResultsService {
	@Autowired
	private EduRequirementUserSurveyResultsMapper eduRequirementUserSurveyResultsMapper;
	@Autowired
	private EduRequirementItemsSurveyResultsMapper eduRequirementItemsSurveyResultsMapper;
	@Autowired
	EduRequirementInvestigationMapper eduRequirementInvestigationMapper;
	@Autowired
	EduRequirementSurveyItemMapper eduRequirementSurveyItemMapper;
		
	@Transactional(rollbackFor = {Throwable.class})
	  public Result add(EduRequirementUserSurveyResults eduRequirementUserSurveyResults){
		        Result result=new Result();
		        //用户ID和需求调查ID必须有
		        if(ObjectUtil.isNull(eduRequirementUserSurveyResults)
		        		||ObjectUtil.isNull(eduRequirementUserSurveyResults.getReqInvestId())
		        		||ObjectUtil.isNull(eduRequirementUserSurveyResults.getUserId())
		        		){		            
		        	result.setFalse("用户ID和需求调查ID字段不能为空");
		            return result;
		        }
		        //id为空或数据库中不存在则插入数据，否则更新
		        if(ObjectUtil.isNull(eduRequirementUserSurveyResults.getId())
		        		|| ObjectUtil.isNull(eduRequirementUserSurveyResultsMapper.selectByPrimaryKey(eduRequirementUserSurveyResults.getId()))){
		        	int res=eduRequirementUserSurveyResultsMapper.insertSelective(eduRequirementUserSurveyResults);
		            
			        if(res==0){
			            result.setFalse("插入失败");
			            return result;
			        }
			        result = insertEduRequirementItemsSurveyResults(eduRequirementUserSurveyResults);
			        if(result.isSuccess()==false){
			            return result;
			        }
		        }else{	
		        	EduRequirementUserSurveyResults eduResult = eduRequirementUserSurveyResultsMapper.selectByPrimaryKey(eduRequirementUserSurveyResults.getId());
	        	//更新记录中用户id和需求调查id与数据库中相应主键的记录应当一致，一致则更新，不一致失败。
		        	Integer a = eduResult.getReqInvestId();
		        	Integer b = eduRequirementUserSurveyResults.getReqInvestId();
		        	Integer c =  eduResult.getUserId();
		        	Integer d = eduRequirementUserSurveyResults.getUserId();
//		        	
//		        	boolean flag3 = (a==b);
//		        	boolean flag4 = (c==d);
//
//		        	
//		        	
//		        	boolean flag1 = (eduResult.getReqInvestId().equals(eduRequirementUserSurveyResults.getReqInvestId()));
//		        	boolean flag2 = (eduResult.getUserId().equals(eduRequirementUserSurveyResults.getUserId()));

		        	if(a.equals(b) && c.equals(d)){
		        		
		        		
//			        	//需求调查项目结果正常情况不会为空，为方便测试时不用输项目内容，为空则不更新调查项目，实际为空将更新失败。
		        		if(eduRequirementUserSurveyResults.getEduRequirementItemsSurveyResultss().isEmpty()==false){
		        			int res = eduRequirementItemsSurveyResultsMapper.deleteByUserSurveyResultId(eduRequirementUserSurveyResults.getId());
					        if(res==0){
					            result.setFalse("删除用户需求调查结果失败");
					            return result;
					        }
		        			result = insertEduRequirementItemsSurveyResults(eduRequirementUserSurveyResults);
		        			if(result.isSuccess()==false){
		        	            return result;
		        	        }
		        			res = eduRequirementSurveyItemMapper.updateCountItemsByReqInvestId(eduRequirementUserSurveyResults.getReqInvestId());
				    	 	if(res==0){
				                result.setFalse("更新调查结果失败");
				                return result;
				            }
		        		}
//					        else{
//		    	    		result.setFalse("需求调查项目结果不能为空，更新失败");
//		        			return result;
//		        		}
			        	int res=eduRequirementUserSurveyResultsMapper.updateByPrimaryKeySelective(eduRequirementUserSurveyResults);
			        	 if(res==0){
			        		 result.setFalse("更新失败");
					         return result;
			        	 }
			     		 res = eduRequirementSurveyItemMapper.updateCountItemsByReqInvestId(eduRequirementUserSurveyResults.getReqInvestId());
			    	 	if(res==0){
			                result.setFalse("更新调查结果失败");
			                return result;
			            }
			        	 
		        	}else{
		        		result.setFalse("更新失败，待更新记录中用户id和需求调查id与数据库不符");
		        	}
		        }
		        result.setData(eduRequirementUserSurveyResults);

		        return result;
	  }
	  
//	    
	  //插入需求用户调查结果的各项目结果
	    private Result insertEduRequirementItemsSurveyResults(EduRequirementUserSurveyResults eduRequirementUserSurveyResults){
		     Result result=new Result();

//	    	if(CollectionUtil.isEmpty(eduRequirementUserSurveyResults.getEduRequirementItemsSurveyResultss())){
//	    		  result.setFalse("用户需求调查结果为空");q
//		          return result;		    }
	    	List<EduRequirementItemsSurveyResults> eduRequirementItemsSurveyResultss =  eduRequirementUserSurveyResults.getEduRequirementItemsSurveyResultss();
			for(EduRequirementItemsSurveyResults eduRequirementSurveyItem: eduRequirementItemsSurveyResultss){
				eduRequirementSurveyItem.setUserSurveyResultId(eduRequirementUserSurveyResults.getId());
			}
		    int res = eduRequirementItemsSurveyResultsMapper.insertList(eduRequirementUserSurveyResults.getEduRequirementItemsSurveyResultss());
	        if(res==0){
	            result.setFalse("插入失败");
	            return result;
	        }
	        result.setData(eduRequirementUserSurveyResults.getEduRequirementItemsSurveyResultss());

	    	return result;		 

	    }
    
	  public Result delete(Integer id){
	      Result result=new Result();
	      if(ObjectUtil.isNull(id)){
	          result.setFalse("删除对象不能为空");
	          return result;
	      }
	      EduRequirementUserSurveyResults eduRequirementUserSurveyResults= eduRequirementUserSurveyResultsMapper.selectByPrimaryKey(id);
	      int reqInvestId = eduRequirementUserSurveyResults.getReqInvestId();
	      int res = eduRequirementUserSurveyResultsMapper.deleteByPrimaryKey(id);
	      if(res==0){
	          result.setFalse("删除失败");
	          return result;
	      }
	      res = eduRequirementSurveyItemMapper.updateCountItemsByReqInvestId(reqInvestId);
  	 	if(res==0){
              result.setFalse("更新调查结果失败");
              return result;
          }
	      return result;
	  }

	  
	  public Result find(EduRequirementUserSurveyResults eduRequirementUserSurveyResults) {
	      Result result = new Result();
	      PageHelper.startPage(eduRequirementUserSurveyResults.getPageCurrent(), eduRequirementUserSurveyResults.getPageSize());

	      List<EduRequirementUserSurveyResults> eduRequirementUserSurveyResultss = eduRequirementUserSurveyResultsMapper.search(eduRequirementUserSurveyResults);
	      result.setData(new PageInfo<>(eduRequirementUserSurveyResultss));

	      return result;
	  }
	  
	  public Result findDetail(EduRequirementUserSurveyResults eduRequirementUserSurveyResults) {
	      Result result = new Result();
	      PageHelper.startPage(eduRequirementUserSurveyResults.getPageCurrent(), eduRequirementUserSurveyResults.getPageSize());

	      List<EduRequirementUserSurveyResults> eduRequirementUserSurveyResultss = eduRequirementUserSurveyResultsMapper.searchDetail(eduRequirementUserSurveyResults);
	      result.setData(new PageInfo<>(eduRequirementUserSurveyResultss));

	      return result;
	  }
	  //查询已完成填报的需求调查
	  public Result showAdviceRes(EduRequirementUserSurveyResults eduRequirementUserSurveyResults) {
	      Result result = new Result();
	      PageHelper.startPage(eduRequirementUserSurveyResults.getPageCurrent(), eduRequirementUserSurveyResults.getPageSize());

	      List<EduRequirementUserSurveyResults> eduRequirementUserSurveyResultss = eduRequirementUserSurveyResultsMapper.searchDoneDetail(eduRequirementUserSurveyResults);
	      result.setData(new PageInfo<>(eduRequirementUserSurveyResultss));

	      return result;
	  }
	  //查询选择指定项目的结果/人
	  public Result showItemRes(EduRequirementSurveyItem item) {
	      Result result = new Result();
	      PageHelper.startPage(item.getPageCurrent(), item.getPageSize());

	      List<EduRequirementUserSurveyResults> eduRequirementUserSurveyResultss = eduRequirementUserSurveyResultsMapper.showItemRes(item);
	      result.setData(new PageInfo<>(eduRequirementUserSurveyResultss));

	      return result;
	  }
	  

}
